<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row style="height: 60px">
              <el-button
                size="small"
                icon="el-icon-plus"
                type="primary"
                @click="dialogShow = true"
              >新增角色</el-button>
            </el-row>
            <el-table border :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <div slot-scope="{ row }">
                  <el-button size="small" type="success" @click="distribution(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </div>
              </el-table-column>
            </el-table>
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev,pager,next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="企业名称">
                <el-input
                  disabled
                  style="width: 400px"
                  :value="companyInfo.name"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  disabled
                  style="width: 400px"
                  :value="companyInfo.companyAddress"
                />
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input
                  disabled
                  style="width: 400px"
                  :value="companyInfo.companyPhone"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  disabled
                  style="width: 400px"
                  :value="companyInfo.mailbox"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                  :value="companyInfo.remarks"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog
      :title="roleTitle"
      :visible.sync="dialogShow"
      :close="handleClose"
    >
      <el-form ref="roleFrom" :model="roleFrom" :rules="rules" label-width="120px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleFrom.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleFrom.description" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="btnOK">确 定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <el-dialog
      title="分配权限"
      :visible.sync="permDialogShow"
      @close="permClose"
    >
      <el-tree
        ref="permTree"
        :data="permissionList"
        node-key="id"
        :empty-text="'加载中...'"
        :props="{label: 'name'}"
        default-expand-all
        show-checkbox
        check-strictly
        :default-checked-keys="permIds"
      />
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button @click="permClose">取 消</el-button>
          <el-button type="primary" @click="permBtnOK">确 定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getCompanyInfo, getRoleList, delRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Setting',
  data() {
    return {
      list: [],
      companyInfo: {}, // 公司信息
      page: {
        page: 1,
        pagesize: 10,
        total: 0
      },
      dialogShow: false,
      permDialogShow: false,
      roleFrom: {}, // 角色信息弹出层
      rules: { // 表单验证
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      permissionList: [], // 所有权限信息
      permIds: [], // 角色权限列表
      roleId: null
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    roleTitle() {
      return this.roleFrom.id ? '编辑角色' : '新增角色'
    }
  },
  watch: {
    list(newList, oldList) {
      if (newList.length < 1) {
        this.page.page--
        this.getRoleList()
      }
    }
  },
  async created() {
    this.getRoleList()
    this.companyInfo = await getCompanyInfo(this.companyId)
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.list = rows
      this.page.total = total
    },
    changePage(page) {
      this.page.page = page
      this.getRoleList()
    },
    async delRole(id) {
      try {
        await this.$confirm('此操作将永久删除角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delRole(id)
        this.$message.success('删除成功!')
        this.getRoleList()
      } catch (err) {
        console.log(err)
      }
    },
    handleClose() {
      this.dialogShow = false
      this.roleFrom = {}
      this.$refs.roleFrom.resetFields()
    },
    async editRole(id) {
      this.dialogShow = true
      this.roleFrom = await getRoleDetail(id)
    },
    async btnOK() {
      try {
        await this.$refs.roleFrom.validate()
        if (this.roleFrom.id) {
          // 修改操作
          await updateRole(this.roleFrom)
          this.$message.success('修改成功')
        } else {
          // 添加操作
          await addRole(this.roleFrom)
          this.$message.success('添加成功')
        }
        this.getRoleList() // 重新获取最新的角色列表
        this.handleClose() // 关闭弹层 清空数据
      } catch (err) {
        console.log(err)
      }
    },
    async distribution(id) {
      this.roleId = id
      const permissionList = tranListToTreeData(await getPermissionList(), '0')
      this.permissionList = permissionList
      const { permIds } = await getRoleDetail(id)
      this.permIds = permIds
      this.permDialogShow = true
    },
    permClose() {
      this.permIds = []
      this.permDialogShow = false
    },
    async permBtnOK() {
      await assignPerm({ id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() })
      this.$message.success('分配权限成功')
      this.permClose()
    }
  }
}
</script>

<style>
</style>
